package com.matriq.cloudmanager;

import java.util.ArrayList;
import java.util.List;

import com.matriq.cloudmanager.R;
import com.matriq.cloudmanager.entity.RemindNumber;
import com.matriq.cloudmanager.service.AccountService;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView.LayoutParams;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import net.lgpage.activity.BaseActivity;
import net.lgpage.http.SwipeRefeshAsyncTask;
import net.lgpage.util.Cleaner;

public class ReminderActivity extends BaseActivity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ac_reminder);
		setViews();
		setEvents();
		loadData();
	}

	android.support.v4.widget.SwipeRefreshLayout srl;
	android.support.v7.widget.RecyclerView rcy_reminder;
	TextView tv_title;
	ImageButton ll_back;
	LinearLayout ll_nothing;
	TextView tv_nothing;

	private void setViews() {
		srl = (android.support.v4.widget.SwipeRefreshLayout) this.findViewById(R.id.srl);
		rcy_reminder = (android.support.v7.widget.RecyclerView) this.findViewById(R.id.rcy_reminder);
		rcy_adp = new RcyAdp();
		rcy_reminder.setLayoutManager(new LinearLayoutManager(this));
		rcy_reminder.setAdapter(rcy_adp);
		tv_title = (TextView) this.findViewById(R.id.tv_title);
		ll_back = (ImageButton) this.findViewById(R.id.ll_back);
		ll_nothing = (LinearLayout) this.findViewById(R.id.ll_nothing);
		tv_nothing = (TextView) this.findViewById(R.id.tv_nothing);
	}

	private void setEvents() {
		srl.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh() {
				refeshRcyList();
			}
		});
		setNotingEvents();
	}
	private void setNotingEvents(){
		ll_nothing.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				changeListState(false);
				refeshRcyList();
			}
		});
	}
	
	private void checkListNoting(){
		if(rcylist == null||rcylist.size() == 0){
			changeListState(true);
		}else{
			changeListState(false);
		}
	}
	
	private void changeListState(boolean isnothing){
		if(!isnothing){
			ll_nothing.setVisibility(View.GONE);
			srl.setVisibility(View.VISIBLE);
		}else{
			ll_nothing.setVisibility(View.VISIBLE);
			srl.setVisibility(View.GONE);
		}
	}
	Object obj;

	public void setData(Object obj) {
		this.obj = obj;
		tv_title.setText(null);
		tv_nothing.setText(null);
	}

	private class RcyViewHolder extends RecyclerView.ViewHolder {
		ImageView iv;
		TextView tv[] = new TextView[3];

		public RcyViewHolder(View view) {
			super(view);
		}
	}

	RcyAdp rcy_adp;

	class RcyAdp extends RecyclerView.Adapter<RcyViewHolder> {

		LayoutParams lp;

		@Override
		public int getItemCount() {
			if (rcylist == null)
				return 0;
			return rcylist.size();
		}

		public RcyAdp() {
			super();
			lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		}

		@Override
		public void onBindViewHolder(RcyViewHolder h, final int position) {
			final String obj = rcylist.get(position);
			h.tv[1].setText(obj);
			h.itemView.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {

				}
			});
		}

		@Override
		public RcyViewHolder onCreateViewHolder(ViewGroup vgp, final int position) {
			View cv = getLayoutInflater().inflate(R.layout.rcy_reminder, null);
			cv.setLayoutParams(lp);
			RcyViewHolder h = new RcyViewHolder(cv);
			h.iv = (ImageView) cv.findViewById(R.id.iv);
			h.tv[0] = (TextView) cv.findViewById(R.id.tv0);
			h.tv[1] = (TextView) cv.findViewById(R.id.tv1);
			h.tv[2] = (TextView) cv.findViewById(R.id.tv2);
			return h;
		}
	}

	List<String> rcylist;
	RcyListTask rcylistTask;

	public void refeshRcyList() {
		if (rcylistTask != null) {
			rcylistTask.cancel(true);
		}
		rcylistTask = new RcyListTask(this);
		rcylistTask.setSwipeLayout(srl);
		rcylistTask.execute();
	}

	private class RcyListTask extends SwipeRefeshAsyncTask<RemindNumber> {

		public RcyListTask(Context context) {
			super(context);
		}

		@Override
		public RemindNumber doInBackgroundX(String... params) throws Exception {
			return AccountService.getService().remind();
		}

		@Override
		public void onPostExecuteX(RemindNumber result) {
			if (result == null) {
				return;
			} else {
				RemindNumber obj = result;
				rcylist = new ArrayList<>();
				if (obj.getCourse() > 0) {
					rcylist.add("你有" + obj.getCourse() + "门课程未评价");
				}
				if (obj.getTask() > 0) {
					rcylist.add("你有" + obj.getTask() + "门作业未提交");
				}
				if (obj.getLeave() > 0) {
					rcylist.add("你有" + obj.getLeave() + "张假条已批准");
				}
				checkListNoting();
				rcy_adp.notifyDataSetChanged();
			}
		}
	}

	private void loadData() {

		refeshRcyList();
	}

	@Override
	public void onDestroy() {
		Cleaner.cleanTask(rcylistTask);

		super.onDestroy();
	}

}